package com.qian.saas.controller;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

@RestController
public class UpFileController {

    @RequestMapping("/uplode")
    public R upimg(@RequestParam("img") MultipartFile audio, HttpServletRequest request) {
        String name = audio.getOriginalFilename();
        try {
            // 将上传的音频文件保存到本地
            // String fileName = audio.getOriginalFilename();

            String lujing = request.getServletContext().getRealPath("file/");

            File file = new File(lujing);
            if (!file.exists()) {

                file.mkdirs();

            }
            String path = lujing + name;
            System.out.println(path);
            OutputStream os = new FileOutputStream(new File(path));
            IOUtils.copy(audio.getInputStream(), os);
            os.close();
            // 返回成功的响应
            return R.success("成功");
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("失败");
        }
    }
}
